package com.wugh.fast.module.async.service.impl;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.wugh.fast.kernel.core.exception.FastBusinessException;
import com.wugh.fast.kernel.util.CopyUtils;
import com.wugh.fast.module.async.dto.*;
import com.wugh.fast.module.async.enums.TaskStatusEnum;
import com.wugh.fast.module.async.service.TaskService;
import com.wugh.fast.module.async.storage.TaskStorage;

import java.util.Date;
import java.util.List;

/**
 * 说明:
 *
 * @author wugh
 * @date 2020/4/10 16:55
 */
public class DefaultTaskService implements TaskService {
    private TaskStorage taskStorage;

    public DefaultTaskService(TaskStorage taskStorage) {
        this.taskStorage = taskStorage;
    }

    @Override
    public TaskProcessDetailDTO getTaskDetail(String taskId) {
        Task task = taskStorage.queryTask(taskId);
        if (task == null) {
            return null;
        }
        TaskProcessDetailDTO dto = CopyUtils.copyObject(task, TaskProcessDetailDTO.class);
        if (TaskStatusEnum.PROCESSING.getCode().equals(task.getStatus())) {
            List<TaskProcessing> processing = taskStorage.queryProcessing(taskId);
            dto.setProcessings(processing);
        }
        return dto;
    }

    @Override
    public Task startTask(CreateTaskDTO createTask) {
        Task task = new Task();
        task.setId(IdUtil.fastUUID());
        task.setName(createTask.getName());
        task.setStatus(TaskStatusEnum.PROCESSING.getCode());
        task.setType(createTask.getType());
        task.setTotal(createTask.getCount());
        task.setProcess(0L);
        task.setDesc("创建并启动");
        task.setGmtInit(new Date());
        task.setGmtStart(task.getGmtInit());
        task.setGmtEnd(null);
        taskStorage.insertTask(task);
        return task;
    }

    @Override
    public void finishTask(String id) {
        Task task = taskStorage.queryTask(id);
        if (!TaskStatusEnum.PROCESSING.getCode().equals(task.getStatus())) {
            throw new FastBusinessException("任务非进行中状态");
        }
        task.setGmtEnd(new Date());
        task.setDesc("任务已完成");
        task.setProcess(task.getTotal());
        task.setStatus(TaskStatusEnum.FINISH.getCode());
        taskStorage.updateTask(task);
    }

    @Override
    public void stopTask(String id, Throwable throwable) {
        Task task = taskStorage.queryTask(id);
        task.setGmtEnd(new Date());
        task.setDesc(StrUtil.format("任务异常终止:{}", ExceptionUtil.getRootCauseMessage(throwable)));
        task.setStatus(TaskStatusEnum.EXCEPTION.getCode());
        taskStorage.updateTask(task);
    }

    @Override
    public void startProcess(CreateProcessDTO createProcessDTO) {
        TaskProcessing processing = new TaskProcessing();
        processing.setTaskId(createProcessDTO.getTaskId());
        processing.setGmtInit(new Date());
        if (createProcessDTO.getData() instanceof String) {
            processing.setData((String) createProcessDTO.getData());
        } else {
            processing.setData(JSON.toJSONString(createProcessDTO.getData()));
        }
        processing.setOperator(createProcessDTO.getOperator());
        taskStorage.updateProcess(processing);
    }

    @Override
    public void finishProcess(String taskId, String operator) {
        taskStorage.deleteProcess(taskId, operator);
        taskStorage.increaseProcess(taskId);
    }
}
